﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;

namespace MSBuild.Mercurial.CodePlex
{
    /// <summary>
    /// Defines the entry point of the application.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The path of the xslt resource used to create the final text file.
        /// </summary>
        private const string TransformPath = "MSBuild.Mercurial.CodePlex.transform.xsl";

        /// <summary>
        /// Generates the CodePlex documentation associated to the MSBuild
        /// tasks of the MSBuild Mercurial Tasks project.
        /// </summary>
        public static void Main()
        {
            XmlDocument document = CreateDocument();
            XslCompiledTransform transform = RetrieveTransform();

            using (Stream stream = new FileStream("output.txt", FileMode.Create, FileAccess.Write))
            {
                transform.Transform(document, null, stream);
            }
        }

        /// <summary>
        /// Creates the xml document associated with the known MSBuild tasks.
        /// </summary>
        /// <returns>
        /// The xml document associated with the known MSBuild tasks.
        /// </returns>
        private static XmlDocument CreateDocument()
        {
            IEnumerable<Type> tasks = DocumentationBuilder.RetrieveTasks(typeof(AbstractHgTask).Assembly);
            TaskDocumentation[] documentations = tasks.Select(t => DocumentationBuilder.CreateDocumentation(t)).ToArray();

            using (TextWriter writer = new StringWriter(CultureInfo.CurrentCulture))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TaskDocumentation[]));
                serializer.Serialize(writer, documentations);

                // Write the data in an xml file for reference
                File.WriteAllText("output.xml", writer.ToString());

                XmlDocument result = new XmlDocument();
                result.LoadXml(writer.ToString());

                return result;
            }
        }

        /// <summary>
        /// Retrieves the xslt from the resource of the application.
        /// </summary>
        /// <returns>
        /// The xslt used to create the final text file.
        /// </returns>
        private static XslCompiledTransform RetrieveTransform()
        {
            XslCompiledTransform transform = new XslCompiledTransform();

            Assembly assembly = typeof(Program).Assembly;
            using (Stream resource = assembly.GetManifestResourceStream(TransformPath))
            {
                transform.Load(new XmlTextReader(resource));
            }

            return transform;
        }
    }
}
